const app = getApp();
const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    searchForm: {
      pageNumber: 1, // 当前页数
      pageSize: 10, // 页面大小
      sort: 'praiseCount',
      order: 'desc',
    },
    totalPages: 0,
    loading: false, //是否正在加载数据
    list: [],
    praiseData: [], //投票的id数组

  },
  //当前页发生改变
  changePage(v) {
    this.setData({
      'searchForm.pageNumber': v
    });
    this.getVoteItemData();
  },
  //查询方法
  handleSearch() {
    this.setData({
      'searchForm.pageNumber': 1
    });
    this.getVoteItemData();
  },
  //获取投票活动数据
  getVoteItemData: function() {
    let that = this;
    that.setData({
      loading: true
    })
    //加载信息
    util.request(api.VOTE_ITEM_LIST, that.data.searchForm).then(function(res) {
      if (res.success === true) {
        let data = new Object();
        let list = [];
        if (that.data.searchForm.pageNumber > 1) { //加载更多  追加信息
          list = that.data.list;
        }
        list.push(...res.result.content)
        data.list = list;
        data.totalPages = res.result.totalPages;
        data.loading = false;
        that.setData(data);
      }
    });

  },
  //预览拖
  previewImage(e) {
    wx.previewImage({
      current: e.currentTarget.dataset.url,
      urls: [e.currentTarget.dataset.url],
    })
  },
  //获取用户投票信息
  getPraise() {
    let that = this;
    util.request(api.GET_PRAISE + '/' + this.data.voteId).then(function(res) {
      if (res.success === true) {
        let data = new Object();
        data.praiseData = res.result;
        that.setData(data);
      }
    });
  },
  //投票
  praise: function(e) {
    let id = e.currentTarget.dataset.id;
    let that = this;
    let arr = that.data.praiseData;
    let index = arr.findIndex(item => item === id);
    if (index === -1) {
      //不存在 ---投票

      util.request(api.PRAISE + '/' + that.data.voteId + '/' + id).then(function(res) {
        if (res.success === true) {
          arr.push(id)
          let list = that.data.list;
          let i = list.findIndex(item => item.id === id);
          let data = new Object();
          data['list[' + i + ']praiseCount'] = list[i].praiseCount + 1;
          data['praiseData'] = arr;
          that.setData(data);
        } else {
          util.$Message({
            content: res.message,
            type: 'error'
          });
        }

      });
    } else {
      //存在  ---错误提醒
      util.$Message({
        content: '请勿重复投票',
        type: 'error'
      });
    }

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    that.setData({
      'voteId': options.voteId,
      'voteName': options.voteName,
      'voteDate': options.voteDate,
      'searchForm.voteId': options.voteId
    });
    //加载
    this.getVoteItemData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getPraise();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    this.changePage(++this.data.searchForm.pageNumber);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})